package com.itxx.thread6;
//同步方法
public class TicketDemo implements Runnable {
    private int sticket = 100;

    @Override
    public void run() {
        mothed();
    }

    private synchronized void mothed() {
        while (true) {
            synchronized (this) {
                if (sticket <= 0) {
                    System.out.println(Thread.currentThread().getName() + ":票卖完了");
                    break;
                } else  {
                    try {
                        Thread.sleep(10);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在卖" + sticket + "张票");
                    sticket--;
                }
            }
        }
    }
}

class Demo {
    public static void main(String[] args) {
        TicketDemo tt = new TicketDemo();
        Thread t1 = new Thread(tt, "窗口1");
        Thread t2 = new Thread(tt, "窗口2");
        Thread t3 = new Thread(tt, "窗口3");
        t1.start();
        t3.start();
        t2.start();
    }
}

